<?php

/*
 * To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor.
 */

/**
 * Description of admin_menu
 *
 * @author shuai
 */
namespace app\components;
use  yii\base\Widget;
use \yii\helpers\Url;
class admin_menu extends Widget {

    // put your code here
    const M_home = 1;
    const M_set = 2;
    const M_log=3;
    const M_user = 5;
    const M_logout = 6;
    const M_base = 7;
    const M_seo = 8;
    const M_fclass_guanli=9;
    const M_fclass_diquliebiao=10;
    const M_error=11;
    const M_pass=12;
    const M_set_base=13;
    const M_dictionaries=14;
    const M_upfile=15;
    
    
    
    ///以下是项目的 
    const M_order=16;

    public $menu = null;
    private $zi_menu = null;
    public $now = null;
    private $ej_array = []; ///面包系的二级内容

    public function get_zi_menu() {
        if (empty($this->zi_menu)) {
            return [];
        }
        return $this->zi_menu;
    }

    public function set_now($id, $ej_id=null) {
        foreach ($this->menu as $key => $vo) {
            if ($key === $id) {
                $this->now = $id;
                $this->set_list($this->get_list_data($id), $ej_id); // /获取当前的子菜单项,然后设置

                return;
            }
        }
        Fun::showmsg('设置菜单项错误');
    }

    public function get_title() {
        if (empty($this->now)) {
            return '';
        }
        if (isset($this->menu[$this->now]) && isset($this->menu[$this->now]['title'])) {
            return $this->menu[$this->now]['title'];
        } else {
            return '';
        }
    }

    private function get_list_data($id) {
        if (!isset($this->menu[$id])) {
            Fun::showmsg('查找菜单错误');
        } else {
            if (isset($this->menu[$id]['list']))
                return $this->menu[$id]['list'];
            else
                return [];
        }
    }

    public function set_list($array, $ej) {///设置二级菜单内容
        $this->zi_menu = $array;
        if (!empty($this->zi_menu)) {
            $i = 0;
            foreach ($this->zi_menu as $key => $vo) {
                if ($i === 0) {
                    $this->ej_array = $vo;
                }
                $i++;
                if ($key === $ej) {
                    $this->ej_array = $vo;
                    return;
                }
            }
        }
    }

    public function add_zi_menu($array) {
        if ($this->ej_array === null) {
            $this->ej_array = [];
        } else {
            $this->ej_array = $this->ej_array + $array;
        }
        return $this->ej_array;
    }

    public function __construct() {
    
        
        
        
        $this->menu = [
            self::M_home => [
                'title' => '首页',
                'href' => '/admin/view',
                 'list' => [
                    [
                        'title' => '欢迎页',
                        'href'=>'/admin/view/'
                    ],
                    self::M_error=>[
                         'title' => '错误日志',
                        'href'=>'/admin/view/errorlog'
                    ]
                     ,
                     self::M_log=>[
                         'title'=>'操作日志',
                         'href'=>'/admin/view/log'
                     ],
                     [
                        'title' => '清理缓存',
                        'href'=>'/admin/view/delete_cache'
                    ],
                      [
                        'title' => '清理微信令牌缓存',
                        'href'=>'/admin/view/dweixinkey'
                    ],
                     
                     ]
            ],
                  self::M_set => [
                'title' => '设置',
                'href' => '/admin/config/set_base',
                'list' => [
                  self::M_set_base => [
                        'title' => '系统设置',
                        'href' => '/admin/config/set_base'
                    ],
              
                    self::M_seo => [
                        'title' => 'SEO设置',
                        'href' => '/admin/config/m_set_seo'
                    ],
                    self::M_pass => [
                        'title' => '后台密码',
                        'href' => '/admin/config/up_pass'
                    ]
             
                    
                    
                ]
            ], 
            self::M_order=>[
                'title'=>'订单',
                'href'=>'/admin/view/order'
            ],
            
              self::M_user => [
                'title' => '用户',
                'href' => '/admin/view/userlist'
            ],
            self::M_logout => [
                'title' => '退出('.\Yii::$app->user->get_name().')',//'.\Yii::$app->user->identity->username.'
                'href' => '/admin/view/logout'
            ]
        ];
        
        //if(defined('YII_DEBUG') && YII_DEBUG){
            $this->menu[self::M_home]['list'][]=
              [
                        'title' => '开始调试',
                        'href'=>'/admin/view/begin'
                    ];
            $this->menu[self::M_home]['list'][]=
                        [ 
                        'title' => '关闭调试',
                        'href'=>'/admin/view/stop'
                
         ];
      ///  }
 
       // print_r($this->menu);exit;
        
        $this->init_create_url();
    }

    private function init_create_url() {
        foreach ($this->menu as $key => $vo) {
            
            if (isset($this->menu[$key]['list'])) {
                foreach ($this->menu[$key]['list'] as $k => $v) {
                    if (!empty($this->menu[$key]['list'][$k]['href']))
                    {
 /// echo Url::to([$this->menu[$key]['list'][$k]['href']]);exit;                      
                   ///     echo 'HHHHHHHHHHH'.
  $this->menu[$key]['list'][$k]['href'] =
          Url::to([$this->menu[$key]['list'][$k]['href']]);
              
                    }
                }
            }
            
       
            
            if (!empty($this->menu[$key]['href']))
            { 
                $this->menu[$key]['href'] =Url::to([$this->menu[$key]['href']]); //Yii::app()->createUrl($this->menu[$key]['href']);
            }
        }
        
    }

    public function show_menu() {
        if ($this->now == null) {
            $this->set_now(self::M_home);
        }

     return   $this->render('show_menu', array('model' => $this));
    }

    public function z_menu() {
     return   $this->render('z_menu', array('model' => $this));
    }

    private function get_now_title() {
        if (empty($this->now)) {
            return null;
        }
        if (isset($this->menu[$this->now])) {
            return $this->menu[$this->now];
        } else {
            return null;
        }
    }

    public function add_bread($title,$href){
        
        $this->bread[]=['title'=>$title,'href'=>$href];
       // print_r($this->bread);exit;
    }

        private $bread = []; ///面包屑

    public function get_bread_list() {
        $list = [
        ];
        $list[] = $this->get_now_title(); ///获取当前的标题
        if(!empty($this->ej_array))
        $list[]=  $this->ej_array;
        if(!empty($this->bread))
            foreach ($this->bread as $vo){
             $list[]=$vo;
            }
       
        return $list;
    }

    public function show_Bread() {
      return  $this->render('show_bread', array('model' => $this));
    }

}
